#ifndef TEST_MGR_H
#define TEST_MGR_H

#include <uv.h>
#include <string>
#include <stdlib.h>
#include <vector>

class sender;

class test_mgr {
public:
    static test_mgr* get() {
        if (!instance_) {
            instance_ = new test_mgr;
            atexit(destroy);
        }

        return instance_;
    }

    ~test_mgr() = default;
    int msg_per_robot() const;
    long& msg_send() { return this->msg_send_; }
    void add_msg_send_count();
    void add_msg_recv_count();
    void add_msg_recv_cost(long);
    void add_game_start();
    void add_game_end();
    void start_login(int);
    void start_exit_timer();
    void set_send_end();

private:
    test_mgr();
    test_mgr(const test_mgr&) = delete;
    test_mgr& operator=(const test_mgr&) = delete;
    static void print_stress_test_result();
    static void destroy() {
        if (instance_) {
            instance_->print_stress_test_result();
            delete instance_;
            instance_ = nullptr;
        }
    }
    
    static void program_exit(uv_timer_t*);
    static void on_login_timeout(uv_timer_t* handle);

private:
    static test_mgr* instance_;
    uv_loop_t* loop_;
    uv_timer_t login_timer_;
    uv_timer_t exit_timer_;

    int client_num_;
    int login_success_;
    long msg_total_;
    long msg_send_;
    long send_start_; //send msg start time token
    long send_end_; //send msg end time token
    long msg_recv_;
    long double recv_cost_; // recv msg cost total
    double QPS_;
    std::vector<sender*> roles_;
};

#endif //TEST_MGR_H
